package net.steam.easyjoboa.web.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.steam.easyjoboa.biz.EmployeeBiz;
import net.steam.easyjoboa.biz.TicklerBiz;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.Tickler;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class TicklerAction {
	private Tickler tickler;
	private TicklerBiz ticklerBiz;
	private Employee employee;
	private EmployeeBiz employeeBiz;
	private List<Tickler> ticklerList;
	private String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	private String[] delChose; // 页面checkbox name
	private String startDate;
	private String endDate;

	/**
	 * 添加日程
	 * 
	 * @return
	 */
	@Action(value = "addSchedule", results = {
			@Result(name = "success", type = "redirect", location = "addSuccess"),
			@Result(name = "error", location = "/error.jsp") })
	public String addSchedule() {
		employee = employeeBiz.findEmployeeById(employee.getUserId());
		tickler.setEmployee(employee);
		int rows = ticklerBiz.add(tickler);
		if (rows != 0) {
			return "success";
		} else {
			return "error";
		}
	}

	/**
	 * 添加日程成功跳转
	 * 
	 * @return
	 */
	@Action(value = "addSuccess", results = {
			@Result(name = "success", location = "/schedule.jsp"),
			@Result(name = "input", location = "error.jsp") })
	public String addSuccess() {
		employee= (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		employee = employeeBiz.findEmployeeById(employee.getUserId());
		if (null != employee) {
			return "success";
		} else {
			return "input";
		}
	}
	
	/**
	 * 根据id查找-日程
	 * @return
	 */
	@Action(value = "toSchedule", results = {
			@Result(name = "success", location = "/schedule.jsp")})
	public String toSchedule() {
		employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		return "success";
	}

	/**
	 * 根据用户id查找个人的日程
	 * 
	 * @return
	 */
	@Action(value = "getTicklerByUserId", results = {
			@Result(name = "success", location = "/schedule_me.jsp"),
			@Result(name = "error", location = "/error.jsp") })
	public String getTicklerByUserId() {
		employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		ticklerList = ticklerBiz.getTicklerByUserId(employee.getUserId());
		if (null != ticklerList) {
			return "success";
		} else {
			return "error";
		}
	}

	/**
	 * 批量删除日程
	 * 
	 * @return
	 */
	@Action(value = "deleteTickler", results = { @Result(name = "success", type = "redirect", location = "getTicklerByUserId") })
	public String deleteTickler() {
		int id[] = new int[delChose.length];
		for (int i = 0; i < delChose.length; i++) {
			id[i] = Integer.parseInt(delChose[i]);
		}
		ticklerBiz.delete(id);
		return "success";
	}

	/**
	 * 按范围搜索日程
	 */
	@Action(value = "searchSchedule", results = {
			@Result(name = "success", location = "/schedule_me.jsp"),
			@Result(name = "error",  location = "/error.jsp"), })
	public String searchSchedule() {
		employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("employee");
		ticklerList = ticklerBiz.searchSchedule(employee.getUserId(),startDate, endDate);
		if (ticklerList != null) {
			return "success";
		} else {
			return "error";
		}
	}

	// get set
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public Tickler getTickler() {
		return tickler;
	}

	public TicklerBiz getTicklerBiz() {
		return ticklerBiz;
	}

	public void setTicklerBiz(TicklerBiz ticklerBiz) {
		this.ticklerBiz = ticklerBiz;
	}

	public void setTickler(Tickler tickler) {
		this.tickler = tickler;
	}

	public List<Tickler> getTicklerList() {
		return ticklerList;
	}

	public void setTicklerList(List<Tickler> ticklerList) {
		this.ticklerList = ticklerList;
	}

	public String getNow() {
		return now;
	}

	public void setNow(String now) {
		this.now = now;
	}

	public String[] getDelChose() {
		return delChose;
	}

	public void setDelChose(String[] delChose) {
		this.delChose = delChose;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public EmployeeBiz getEmployeeBiz() {
		return employeeBiz;
	}

	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}

}
